//
//  GRRiceOrderDetailVCViewController.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/9/7.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRRiceOrderDetailVCViewController.h"
#import "GRRiceCodeCell.h"
#import "GRRiceStatusCell.h"
#import "GRRiceOrderMsgCell.h"
#import "GRRiceOderDetailGoodCell.h"
#import "GRBuyMsgTableViewCell.h"
#import "GRGoodCountTableViewCell.h"
#import "GRRiceOrderModel.h"
#import "GRStoreAction.h"
#import "GRDetailFootSectionView.h"
#import "GRStoreSectionHeaderView.h"
#import "GROrderCommentViewController.h"
#import "GRBuyDiscountSuccessVC.h"
#import "GRReturnReasonView.h"
#import "GRSubmitOrderVC.h"
@interface GRRiceOrderDetailVCViewController ()<UITableViewDelegate,UITableViewDataSource,GRRiceCodeCellDelegate,GRRiceStatusCellDelegate,GRReturnReasonViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *detailList;
@property(nonatomic,strong)NSArray * cellArr;
@property(nonatomic,strong)GRRiceOrderModel * orderModel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

@implementation GRRiceOrderDetailVCViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = KGMTHEM_COLOR;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.detailList.tableHeaderView = self.headerView;
    [self registTableViewCellCalass];
    [self requestData];
}

-(void)requestData{
    [GRStoreAction getRiceOrderDetailPage:self.orderId ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            NSLog(@"%@",responseObject);
            self.orderModel = [GRRiceOrderModel yy_modelWithDictionary:responseObject];
            self.titleLabel.text = self.orderModel.stateStr;
            if ([self.orderModel.state isEqualToString:@"pay"]||[self.orderModel.state isEqualToString:@"cancel"]) {
               self.cellArr = @[@"GRRiceStatusCell",@"GRRiceOderDetailGoodCell",@"GRGoodCountTableViewCell",@"GRBuyMsgTableViewCell",@"GRRiceOrderMsgCell"];
            }else{
              self.cellArr = @[@"GRRiceCodeCell",@"GRRiceOderDetailGoodCell",@"GRGoodCountTableViewCell",@"GRBuyMsgTableViewCell",@"GRRiceOrderMsgCell"];
            }
          [self.detailList reloadData];
        }
    }];
    
}

-(void)registTableViewCellCalass{
    NSArray *cellClasses = @[GRRiceCodeCell.class,GRRiceStatusCell.class,GRRiceOderDetailGoodCell.class,GRBuyMsgTableViewCell.class,GRGoodCountTableViewCell.class,GRRiceOrderMsgCell.class];
    for (Class cls in cellClasses) {
        NSString * identifier = NSStringFromClass(cls);
        UINib * cellNib = [UINib nibWithNibName:identifier bundle:[NSBundle mainBundle]];
        if (cellNib) {
            [self.detailList registerNib:cellNib forCellReuseIdentifier:identifier];
        }else{
            [self.detailList registerClass:cls forCellReuseIdentifier:identifier];
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * idetifier = self.cellArr[indexPath.section];
    UITableViewCell <GoodsDetailCellDelegate> * cell = [tableView dequeueReusableCellWithIdentifier:idetifier forIndexPath: indexPath];
    if ([cell respondsToSelector:@selector(setDelegate:) ]) {
        cell.delegate = self;
    }
    if ([idetifier isEqualToString:@"GRGoodCountTableViewCell"]) {
        [cell updateWithModel:self.orderModel.discountDetail[indexPath.row] atIndexPath:indexPath inView:tableView];

    }else if([idetifier isEqualToString:@"GRBuyMsgTableViewCell"]){
        [cell updateWithModel:self.orderModel.buyNotice[indexPath.row] atIndexPath:indexPath inView:tableView];

    }else{
        [cell updateWithModel:self.orderModel atIndexPath:indexPath inView:tableView];
    }
  
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      NSString * idetifier = self.cellArr[section];
    if ([idetifier isEqualToString:@"GRGoodCountTableViewCell"]) {
        return self.orderModel.discountDetail.count;
    }else if([idetifier isEqualToString:@"GRBuyMsgTableViewCell"]){
        return self.orderModel.buyNotice.count;
    }else{
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSString * idetifier = self.cellArr[indexPath.section];
    if([idetifier isEqualToString:@"GRBuyMsgTableViewCell"]){
        return [GRBuyMsgTableViewCell getCellHeightWithModel:self.orderModel.buyNotice[indexPath.row] ];
    }else{
        return [self.orderModel getCellHeightWith:idetifier];
    }
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSString * idetifier = self.cellArr[section];
    GRStoreSectionHeaderView *sectionHeader = [GRStoreSectionHeaderView xx_instantiateFromNib];
    sectionHeader.rightOffset.constant =10;
    sectionHeader.leftOffset.constant = 10;
    
    if ([idetifier isEqualToString:@"GRGoodCountTableViewCell"]) {
        sectionHeader.titleLabel.text = @"商品详情";
        return sectionHeader;
    }else if ([idetifier isEqualToString:@"GRRiceOrderMsgCell"]){
        sectionHeader.titleLabel.text = @"订单详情";
        return sectionHeader;
    }else if ([idetifier isEqualToString:@"GRBuyMsgTableViewCell"]){
        sectionHeader.titleLabel.text = @"购买须知";
        return sectionHeader;
    }else{
        return nil;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    NSString * idetifier = self.cellArr[section];
    if ([idetifier isEqualToString:@"GRGoodCountTableViewCell"]||[idetifier isEqualToString:@"GRRiceOrderMsgCell"]||[idetifier isEqualToString:@"GRBuyMsgTableViewCell"]) {
     return 45;
    }else{
        return 0.0000000000001;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
       NSString * idetifier = self.cellArr[section];
    if ([idetifier isEqualToString:@"GRGoodCountTableViewCell" ]) {
        GRDetailFootSectionView * footView = [GRDetailFootSectionView  xx_instantiateFromNib];
        footView.rightOffset.constant =10;
       footView.leftOffset.constant = 10;
        footView.discoutLabel.text = [NSString stringWithFormat:@"优惠价：%.2lf元",self.orderModel.discountPrice];
        
        footView.totalLabel.text = [NSString stringWithFormat:@"总价值：%.2lf元",self.orderModel.totalPrice];
        return footView;
    }else{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KscreenWidth, 10)];
            return view;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    NSString * idetifier = self.cellArr[section];
    if ([idetifier isEqualToString:@"GRGoodCountTableViewCell" ]) {
        return 55;
    }else{
        return 10;
    }
}

-(void)detailOrderWithOrderType:(RiceOrderActionType)orderType orderModel:(GRRiceOrderModel *)orderModel{
    if (orderType == RiceOrderActionTypeCustomerService) {
        [[GRUMengCommon shareUMeng] enterAppletChat:self];
    }else if (orderType == RiceOrderActionTypeComment){
        GROrderCommentViewController * commentVC = [[GROrderCommentViewController alloc]init];
        commentVC.isRiceOrder = YES;
        commentVC.riceModel = orderModel;
        [self.navigationController pushViewController:commentVC animated:YES];
        
    }else if (orderType == RiceOrderActionTypeReturnMoeny){
        [GRReturnReasonView showGoodView:self DataSource: [NSMutableArray arrayWithArray:orderModel.cancelReason] OrderId:orderModel.id IsRiceOrder:YES IsCancle:NO];
        
    }else if (orderType == RiceOrderActionTypeCancleOrder){
        [GRReturnReasonView showGoodView:self DataSource: [NSMutableArray arrayWithArray:orderModel.cancelReason] OrderId:orderModel.id IsRiceOrder:YES IsCancle:YES];
        
    }else{//支付
        GRSubmitOrderVC * submitVC = [[GRSubmitOrderVC alloc]init];
        submitVC.orderId = orderModel.id;
        submitVC.isOrder = YES;
        [self.navigationController pushViewController:submitVC animated:YES];
    }


}

-(void)submitCancleReason:(NSString *)reson OrderId:(NSInteger)orderId sCancle:(BOOL)isCancle{//申请退款
    NSDictionary * dic = @{
                           @"cancelReason":reson,
                           @"id":@(orderId)
                           };
    
    if (isCancle) {
        [GRStoreAction RiceCancleOrder:dic ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
            if (success) {
                GRBuyDiscountSuccessVC *successVC = [[GRBuyDiscountSuccessVC alloc]init];
                successVC.type = BuyDiscountTypeCancleOrder;
                 successVC.orderId = self.orderId;
                [self.navigationController pushViewController:successVC animated:YES];
                [self requestData];
            }
        }];
    }else{
        
        [GRStoreAction RiceOrderReturnMoney:dic ComplectionHander:^(BOOL success, id responseObject, GRError *error) {
            if (success) {
                GRBuyDiscountSuccessVC *successVC = [[GRBuyDiscountSuccessVC alloc]init];
                successVC.type = BuyDiscountTypeReturnMoeny;
                successVC.orderId = self.orderId;
                [self requestData];
                [self.navigationController pushViewController:successVC animated:YES];
            }
        }];
    }
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.cellArr.count;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
